package com.example.zzpneo4j.controller;

import com.example.zzpneo4j.dto.Entity;
import com.example.zzpneo4j.dto.Result;
import com.example.zzpneo4j.service.Neo4jService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("neo4j")
public class Neo4jController {
    @Resource
    Neo4jService neo4jService;

    @PostMapping("load")
    public Result loadCsv(MultipartFile file, String labels) throws IOException {
        if (StringUtils.isBlank(labels)) {
            return Result.fail("标签集合不能为空：（xxx:xxx:...）");
        }
        List<Entity> entities = neo4jService.loadCsv(file, labels);
        return Result.success(entities);
    }

    @RequestMapping("/load2")
    public Result uploadExcelToNeo4jData2(MultipartFile file) throws IOException {
        return neo4jService.uploadExcelToNeo4jData2(file);
    }

    @PostMapping("getSubNodesTree")
    public Result loadCsv(String nodeType) {
        List entities = neo4jService.query(nodeType);
        return Result.success(entities);
    }

    @PostMapping("getAllLabels")
    public Result getAllLabels(@PathVariable(required = false) String nodeName) {
        List entities = neo4jService.getAllLabels();
        return Result.success(entities);
    }

    @PostMapping("getNodes")
    public Result getNodes(String nodeId, String nodeName) {
        List entities = neo4jService.getNodes(nodeId, nodeName);
        return Result.success(entities);
    }

    @PostMapping("getNodesByRelationShip")
    public Result getNodesByRelationShip(String relationShip) {
        Map<String, Object> entities = neo4jService.getNodes(relationShip);
        return Result.success(entities);
    }


}
